package com.ccnu.demojava.concurrent.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class RThreadPool {
    public static class RThreadPoolRunnable implements Runnable{
    	private String runName;		// 执行体名称
    	
        /**
		 * @param runName
		 */
		public RThreadPoolRunnable(String runName) {
			super();
			this.runName = runName;
		}

		public void run() {
            System.out.println("[" +runName + ":" + Thread.currentThread().threadId() + "]" + "Runnable Hello world!");
        }
    }

    public static class RThreadPoolExceptionRunnable implements Runnable{
        private static int i=0;
        public void run() {
            i++;
            if(i%3==0){
                @SuppressWarnings("unused")
				int j=i/0;
            }
            System.out.println("[" + Thread.currentThread().threadId() + "]" + "Runnable Hello world![" + i + "]");
        }
    }

    // 测试线程池
    public static void FixedPool1(){
        ExecutorService saleTicketsPool = Executors.newFixedThreadPool(10);
        for(int i=0; i<1000; i++){
            @SuppressWarnings("unused")
			final int nTickets = i;
            saleTicketsPool.execute(new Runnable() {
                public void run() {
                    System.out.println("[FixedPool1:" + Thread.currentThread().threadId() + "]" + "Runnable:" + nTickets);
                }
            });
        }
    }

    // 依靠Submit驱动线程池
    public static void FixedPool2(){
        ExecutorService saleTicketsPool = Executors.newFixedThreadPool(10);
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));
        saleTicketsPool.submit(new RThreadPoolRunnable("FixedPool2"));

        saleTicketsPool.shutdown();
    }

    // 使用Cache缓存线程池，理论上线程数量是无限的
    public static void CachedPool(){
        ExecutorService saleTicketsPool = Executors.newCachedThreadPool();

        for(int i=0; i<100 ;i++){
            saleTicketsPool.submit(new RThreadPoolRunnable("CachedPool"));
        }

        saleTicketsPool.shutdown();
    }

    // 延迟执行示例代码
    public static void ScheduledPool(){
        ScheduledExecutorService saleTicketsPool = Executors.newScheduledThreadPool(5);

        for(int i=0; i<100 ;i++){
            saleTicketsPool.schedule(new RThreadPoolRunnable("ScheduledPool"), 5, TimeUnit.SECONDS);
        }
        
        saleTicketsPool.shutdown();
    }

    // 单线程示例
    public static void SingleThreadPool(){
        ExecutorService saleTicketsPool = Executors.newSingleThreadExecutor();

        for(int i=0; i<10 ;i++){
            saleTicketsPool.execute(new RThreadPoolExceptionRunnable());
        }

        saleTicketsPool.shutdown();
    }
}
